# -*- coding:utf-8 -*-
from django.db import models

# Create your models here.
class Arch(models.Model):
    name = models.CharField(help_text="硬件架构", primary_key=True, max_length=10)
    descriptions = models.CharField(help_text="描述", blank=True, max_length=100)
    def __unicode__(self):
        return self.name
    
class Testclass(models.Model):
    TYPES = (('cpu', 'cpu'),
             ('mem', 'mem'),
             ('io', 'io'),
             ('net', 'net')
             )
    name = models.CharField(help_text="测试类型", primary_key=True, max_length=5, choices=TYPES)
    descriptions = models.TextField(help_text="描述", blank=True)
    def __unicode__(self):
        return self.name 

class Testcontent(models.Model):
    name = models.CharField(help_text="测试内容", primary_key=True, max_length=20)   
    testclass = models.ForeignKey(Testclass, help_text="测试类型")
    descriptions = models.TextField(help_text="描述", blank=True)
    def __unicode__(self):
        return self.name
    
class Tool(models.Model):
    name = models.CharField(help_text="测试工具名称", primary_key=True, max_length=20)
    version = models.CharField(help_text="版本号", max_length=10)
    testclass = models.ForeignKey(Testclass)
    testcontent = models.ForeignKey(Testcontent)
    descriptions = models.TextField(help_text="描述", blank=True)
    def _get_tool(self):
        return "%s-%s" % (self.name, self.version)
    tool = property(_get_tool)
    
    def __unicode__(self):
        return self.tool
    
class Unit(models.Model):
    name  = models.CharField(help_text="单位", primary_key=True, max_length=20)
    descriptions = models.CharField(help_text="描述", blank=True, max_length=100)
    def __unicode__(self):
        return self.name
   
class Index(models.Model):
    name = models.CharField(help_text="评价指标", primary_key=True, max_length=20)
    unit = models.ForeignKey(Unit, help_text="单位")
    measure = models.CharField(help_text="评价标准", max_length=30)
    testcontent = models.ForeignKey(Testcontent, help_text="测试内容")
    testclass = models.ForeignKey(Testclass, help_text="测试类型")
    tool = models.ForeignKey(Tool, help_text="测试工具")
    descriptions = models.TextField(help_text="描述", blank=True)
    def __unicode__(self):
        return self.name 
    
class Hardware(models.Model):
    name = models.CharField(help_text="硬件型号", max_length=100)
    cpu=models.CharField(help_text="cpu信息", max_length=100)
    memory = models.CharField(help_text="mem信息", max_length=100)
    disk = models.CharField(help_text="磁盘信息", max_length=100)
    network = models.CharField(help_text="网络信息", max_length=100)
    others = models.TextField(help_text="其他信息" , blank=True)
    def __unicode__(self):
        return self.name
      
class Product(models.Model):
    name = models.CharField(help_text="产品名称", max_length=50)
    release = models.CharField(help_text="发布版本号", max_length=10)
    version = models.CharField(help_text="build版本号", max_length=20, blank=True)
    arch = models.ForeignKey(Arch)
    vendor = models.CharField(help_text="公司", max_length=50)
    descriptions = models.TextField(help_text="描述", blank=True)
    def _get_os(self):
        if self.version:
            return "%s-%s-%s(%s)" % (self.name , self.release, self.version, self.arch)
        else:
            return "%s-%s(%s)" % (self.name, self.release, self.arch)
   
    os = property(_get_os)
    
    class Meta:
        unique_together = (
                         ('name', 'release', 'version', 'arch'),
                         )
        
    def __unicode__(self):
        return self.os
    
   
class Tester(models.Model):    
    name = models.CharField(help_text="测试员", primary_key=True, max_length=20)
    position = models.CharField(help_text="职称", max_length=20, blank=True)
    company = models.CharField(help_text="公司名称", max_length=30, blank=True)
    mail = models.EmailField(blank=True)
    telephone = models.CharField(help_text="电话号码", max_length=15, blank=True)
    def __unicode__(self):
        return self.name
        
class Scene(models.Model):
    name = models.CharField(help_text="测试场景名称", primary_key=True, max_length=20)
    testcontent = models.ForeignKey(Testcontent, help_text="测试内容")
    testclass = models.ForeignKey(Testclass, help_text="测试分类")
    tool = models.ForeignKey(Tool, help_text="测试工具")
    parallels = models.CharField(help_text="并行数", max_length=30)
    times = models.IntegerField(help_text="测试次数")
    mainparas = models.CharField(help_text="主要测试参数", max_length=100, blank=True)
    descriptions = models.TextField(help_text="描述", blank=True)
    def __unicode__(self):
        return self.name
    
class Scenegroup(models.Model):
    name = models.CharField(help_text="场景组名称",  primary_key=True, max_length=20 )
    scenes = models.ManyToManyField(Scene)
    descriptions = models.TextField(help_text="描述", blank=True)
    def __unicode__(self):
        return self.name 

class Job(models.Model):
    jobid = models.CharField(help_text="任务ID", primary_key=True, max_length=14)
    name = models.CharField(help_text="任务名称", max_length=20)
    hardware = models.ForeignKey(Hardware)
    os = models.ForeignKey(Product, help_text="操作系统")
    scenegroup = models.ForeignKey(Scenegroup)
    tester = models.ForeignKey(Tester)
    time = models.DateTimeField()
    def __unicode__(self):
        return self.jobid
 
class Environ(models.Model):
    job = models.OneToOneField(Job, primary_key=True)
    kernel=models.CharField(help_text="内核信息", max_length=150)
    kernelcmd = models.CharField(help_text="内核参数", max_length=300)
    runlevel = models.IntegerField(help_text="运行级别")
    filesystem = models.CharField(help_text="文件系统信息", max_length=100)
    swapsize = models.CharField(help_text="swap大小", max_length=20)
    packages = models.IntegerField(help_text="安装包数量")
    gcc = models.CharField(help_text="gcc信息", max_length=100, blank=True)
    glibc = models.CharField(help_text="glibc信息", max_length=100, blank=True)
    java = models.CharField(help_text="java信息", max_length=100, blank=True)
    selinux= models.CharField(help_text="selinux信息", max_length=10, blank=True)
    iptables = models.CharField(help_text="iptables信息", max_length=10, blank=True) 
    services = models.TextField(help_text="服务列表", blank=True)
    others = models.TextField(help_text="其他", blank=True)
    
     
class Result(models.Model):
    index = models.ForeignKey(Index, help_text="评价指标")
    job = models.ForeignKey(Job)
    scene = models.ForeignKey(Scene)
    parallel = models.IntegerField()
    class Meta:
        unique_together = (
                        ('index', 'job', 'scene', 'parallel'),)
    def __unicode__(self):
        return  "%s_%s_%d"  % (self.index, self.job, self.parallel)
    
class Value(models.Model):
    result = models.ForeignKey(Result)
    iter = models.IntegerField()
    value = models.DecimalField(decimal_places=3, max_digits=15)
    class Meta:
        unique_together = (
                           ('result', 'iter'),)
    
    
    